/**
 * Created by sxx on 2017/5/1.
 */
import "./index.less";
import httpurl from "../../../unit/common/js/httpurl"

let splrId = sessionStorage.getItem('splrId');
let fullNam = sessionStorage.getItem('fullNam');
if(fullNam !=null && fullNam !='' && fullNam !='null'){
    $(".J_splrNam").val(fullNam);
}
let token = sessionStorage.getItem('supplier_token');
if(!token){
    alert('请先登录');
    window.location=HOST_FRT+'/portal/login.html';
} else if(!splrId){
    alert('请先完善注册信息');
    window.location=HOST_FRT+'/supplier_self/user_register.html';
}
let projId = httpurl.uri.params.id;
if(!projId){
    alert("参数无效");
    window.history.back();
}

let fileUpload = '';

jQuery.ajax({
    url: HOST_ADDR+'/cpmp/bidProjOnMngForPortal/viewProjDtl/'+projId,
    type: 'get',
    dataType: 'json',
    contentType:"application/json",
    headers: {
        'access_token': token,
        'login_type': 'splr'
    },
    success: function(resp){
        if (!resp.success) {
            console.log(resp.msgCod+' '+resp.msgInf);
            return;
        }
        if(resp.data.bidProjOn.dpstFlg == null || resp.data.bidProjOn.dpstFlg == '0'){ // 不需要保证金
            $(".J_dpstSts").parent().parent().parent().addClass("hide");
            $(".J_dpstPic").parent().parent().parent().addClass("hide");
        }
        console.log(resp);
    },
    error:function(err){
        console.log(err);
    }
});

$('.J_add-user-form').validate({
    errorElement: 'label', //default input error message container
    errorClass: 'help-inline', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        splrCtct: {
            required: true
        },
        splrCtctTel: {
            required: true
        },
        splrNam:{
            required: true
        }
    },
    message:{

    },

    invalidHandler: function (event, validator) { //display error alert on form submit
        $('.alert-error', $('.J_add-user-form')).show();
    },

    highlight: function (element) { // hightlight error inputs
        $(element).closest('.control-group').addClass('error'); // set error class to the control group
    },

    success: function (label) {
        label.closest('.control-group').removeClass('error');
        label.remove();
    },

    errorPlacement: function (error, element) {
        error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
    },

    submitHandler: function (form) {
        let $form = $(form);
        let splrCtct = $form.find('.J_splrCtct')[0].value;
        let splrCtctTel = $form.find('.J_splrCtctTel')[0].value;
        let splrNam = $form.find('.J_splrNam')[0].value;
        let memo = $form.find('.J_memo')[0].value;
        let dpstSts = $form.find('.J_dpstSts')[0].value;
        // let dpstPic = $form.find('.J_dpstPic')[0].files[0];
        if (dpstSts==1&&!fileUpload) {
            alert('请先上传图片');
            return;
        }
        console.log(dpstSts);
        let data = {
            "projId":projId,
            "splrId":splrId,
            "splrCtct":splrCtct,
            "splrCtctTel":splrCtctTel,
            "splrNam":splrNam,
            "dpstSts":dpstSts,
            "dpstPic":fileUpload,
            "memo":memo
        }

        $.ajax({
            url: HOST_ADDR + '/cpmp/bidProjOnMngForSplr/bidApp',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            access_token: token,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp){
                console.log(resp);
                if(resp.success){
                    alert('申请成功');
                    window.history.back();
                }
                else{
                    alert(resp.msgInf);
                    window.history.back();
                }
            },
            error:function(err){
                console.log(err);
            }
        });
    }
});

$('.J_add-user-form .J_cancel').click(function(){
    location.href = 'proj_online_list.html';
});

$('.J_imgUpload').on('click', function() {
    let formData = new FormData();
    let file = $('.J_dpstPic')[0].files[0];
    console.log(file);
    if (file) {
        formData.append('file', file);
        $.ajax({
            url: HOST_ADDR + '/cpmp/file/upload/img',
            type: 'post',
            data: formData,
            processData: false,
            contentType: false,
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            success: function(resp) {
                if (resp.success) {
                    fileUpload = resp.data;
                    alert('上传图片成功');
                } else {
                    alert('上传图片失败');
                }
            },
            error: function(err) {
                console.log(err);
            }
        });
    } else {
        alert('请先上传图片');
    }
});